﻿using Handday.Erp.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace Handday.Erp.Transfer
{
    /// <summary>
    /// 分表信息中间件
    /// </summary>
    public static class SplitTableMiddlewareExtension
    {
        /// <summary>
        /// 分表中间件
        /// </summary>
        public static IApplicationBuilder UserSplitTable(this IApplicationBuilder builder)
        {
            builder.UseMiddleware<SplitTableMiddleware>();
            return builder;
        }
    }

    /// <summary>
    /// 分表信息中间件
    /// </summary>
    public class SplitTableMiddleware
    {
        private readonly RequestDelegate _next;

        /// <summary>
        /// ctor
        /// </summary>
        public SplitTableMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        /// <summary>
        /// invoke
        /// </summary>
        public async Task InvokeAsync(HttpContext context, CurrentInfo currentInfo, ISplitTableInfo splitTableInfo)
        {
            if (currentInfo.CorpId > 0)
            {
                splitTableInfo.Suffix = currentInfo.CorpId.ToString();
            }

            await _next(context);
        }
    }
}
